package
{
	/**
	 * 日期转换工具
	 * @author billowa
	 * 
	 */	
	public class BDate
	{
		/** */
		private static const defaultDayLables:Array = ["星期天" ,"星期一" ,"星期二" ,"星期三" ,"星期四" ,"星期五" ,"星期六"];
		
		
		/** 年 */
		public static var fullYear:String;
		/** 月 */
		public static var month:String;
		/** 日 */
		public static var date:String;
		/** 星期(或  天数) */
		public static var day:String;
		/** 小时 */
		public static var hour:String;
		/** 分 */
		public static var minute:String;
		/** 秒 */
		public static var second:String;
		
		
		
		
		/**
		 * 倒计时 
		 * @param date1 当前时间
		 * @param date2 目标时间
		 * 
		 */	
		public static function theCountdown (date1:Date ,date2:Date):String
		{
			var temp:Number=(date2.getTime()-date1.getTime());
			//天
			var dayCount:Number = Math.floor(temp / (1000 * 60 * 60 * 24));   
			temp = temp -  dayCount * (1000 * 60 * 60 * 24); 
			//小时
			var hourCount:Number = Math.floor( temp / (1000 * 60 * 60) );
			temp = temp - hourCount * (1000 * 60 * 60 );
			//分
			var minCount:Number = Math.floor( temp / (1000 * 60) );
			temp = temp - minCount * ( 1000 * 60 );
			//秒
			var secCount:Number = Math.floor( temp / 1000 );
			
			day = dayCount+"";
			hour = hourCount+"";
			minute = minCount+"";
			second = secCount+"";
			
			return day+"天 "+hour+":"+minute+""+second;
		}
		
		
		
		
		
		/**
		 * 将秒转为小时制 
		 * @param time
		 * @return 
		 * 
		 */			
		public static function formatSecond(val:Number):String
		{
			var new_hour:uint = val / 60 / 60 >> 0;
			var new_minute:uint = (val / 60) % 60;
			var new_second:uint = val % 60;
			
			hour = new_hour > 100 ? new_hour.toString() : (100 + new_hour).toString().substr(1);
			minute = formatNumber(new_minute);
			second = formatNumber(new_second);
			
			return hour+":"+minute+":"+second;
		}
		
		
		
		
		/**
		 * 标准日期
		 * @param val 当前时间
		 * @param dayLables 默认为 ["星期天" ,"星期一" ,"星期二" ,"星期三" ,"星期四" ,"星期五" ,"星期六"];
		 * @return 
		 * 
		 */			
		public static function formatDate(val:Date ,dayLables:Array=null):String
		{
			fullYear = val.fullYear+"";
			month = val.month+1+"";
			date = val.date+"";
			day = dayLables!=null?dayLables[val.getDay()]:defaultDayLables[val.getDay()];
			hour = formatNumber(val.hours);
			minute = formatNumber(val.minutes);
			second = formatNumber(val.seconds);
			return fullYear + "-" + month + "-" + date + " "+ hour + ":" + minute + " " + day;
		}
		
		
		
		/**
		 * 格式化单位
		 * @param num
		 * @return 
		 * 
		 */		
		private static function formatNumber(num:Number):String
		{
			return num<10?"0"+num:""+num;
		}
	}
}